home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / v cisle / sadanastroju / lightning-0.8-tb-win.xpi / chrome / calendar.jar / content / calendar / calendar-task-view.js < prev    next >
Text File  |  2008-02-19  |  13KB  |  304 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Sun Microsystems code.
  15.  *
  16.  * The Initial Developer of the Original Code is Sun Microsystems.
  17.  * Portions created by the Initial Developer are Copyright (C) 2006
  18.  * the Initial Developer. All Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *   Michael Buettner <michael.buettner@sun.com>
  22.  *   Philipp Kewisch <mozilla@kewis.ch>
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  26.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the MPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the MPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37.  
  38. var taskDetailsView = {
  39.  
  40.     /**
  41.      * Task Details Events
  42.      */
  43.     onSelect: function tDV_onSelect(event) {
  44.  
  45.         var dateFormatter =
  46.             Components.classes["@mozilla.org/calendar/datetime-formatter;1"]
  47.             .getService(Components.interfaces.calIDateTimeFormatter);
  48.  
  49.         var displayElement = function(id,flag) {
  50.             var element = document.getElementById(id);
  51.             if (element) {
  52.                 if (flag) {
  53.                     element.removeAttribute("hidden");
  54.                 } else {
  55.                     element.setAttribute("hidden", "true");
  56.                 }
  57.             }
  58.             return flag;
  59.         }
  60.  
  61.         var item = document.getElementById("calendar-task-tree").currentTask;
  62.         if (displayElement("calendar-task-details",item != null)) {
  63.             displayElement("calendar-task-details-title-row", true);
  64.             document.getElementById("calendar-task-details-title").value = item.title;
  65.             var organizer = item.organizer;
  66.             if (displayElement("calendar-task-details-organizer-row", organizer != null)) {
  67.                 var name = organizer.commonName;
  68.                 if (!name || name.length <= 0) {
  69.                   if (organizer.id && organizer.id.length) {
  70.                       name = organizer.id;
  71.                       var re = new RegExp("^mailto:(.*)", "i");
  72.                       var matches = re.exec(name);
  73.                       if (matches) {
  74.                           name = matches[1];
  75.                       }
  76.                   }
  77.                 }
  78.                 if (displayElement("calendar-task-details-organizer-row", name && name.length)) {
  79.                     document.getElementById("calendar-task-details-organizer").value = name;
  80.                 }
  81.             }
  82.             var priority = 0;
  83.             if (item.calendar.getProperty("capabilities.priority.supported") != false) {
  84.                 priority = parseInt(item.priority);
  85.             }
  86.             if (displayElement("calendar-task-details-priority-row", priority > 0)) {
  87.                 if (priority >= 1 && priority <= 4) {
  88.                     displayElement("calendar-task-details-priority-low", false);
  89.                     displayElement("calendar-task-details-priority-normal", false);
  90.                     displayElement("calendar-task-details-priority-high", true);
  91.                 } else if (priority == 5) {
  92.                     displayElement("calendar-task-details-priority-low", false);
  93.                     displayElement("calendar-task-details-priority-high", false);
  94.                     displayElement("calendar-task-details-priority-normal", true);
  95.                 } else if (priority >= 6 && priority <= 9) {
  96.                     displayElement("calendar-task-details-priority-normal", false);
  97.                     displayElement("calendar-task-details-priority-high", false);
  98.                     displayElement("calendar-task-details-priority-low", true);
  99.                 } else {
  100.                     displayElement("calendar-task-details-priority-normal", false);
  101.                     displayElement("calendar-task-details-priority-high", false);
  102.                     displayElement("calendar-task-details-priority-low", false);
  103.                 }
  104.             }
  105.             var status = item.getProperty("STATUS");
  106.             if (displayElement("calendar-task-details-status-row", status && status.length > 0)) {
  107.                 var statusDetails = document.getElementById("calendar-task-details-status");
  108.                 switch(status) {
  109.                     case "NEEDS-ACTION":
  110.                         statusDetails.value = calGetString(
  111.                             "calendar",
  112.                             "taskDetailsStatusNeedsAction");
  113.                         break;
  114.                     case "IN-PROCESS":
  115.                         var percent = 0;
  116.                         var property = item.getProperty("PERCENT-COMPLETE");
  117.                         if (property != null) {
  118.                             var percent = parseInt(property);
  119.                         }
  120.                         statusDetails.value = calGetString(
  121.                             "calendar",
  122.                             "taskDetailsStatusInProgress", [percent]);
  123.                         break;
  124.                     case "COMPLETED":
  125.                         if (item.completedDate) {
  126.                             statusDetails.value = calGetString(
  127.                                 "calendar",
  128.                                 "taskDetailsStatusCompletedOn",
  129.                                 [dateFormatter.formatDateTime(item.completedDate)]);
  130.                         }
  131.                         break;
  132.                     case "CANCELLED":
  133.                         statusDetails.value = calGetString(
  134.                             "calendar",
  135.                             "taskDetailsStatusCancelled");
  136.                         break; 
  137.                     default:
  138.                         displayElement("calendar-task-details-status-row", false);
  139.                         break;
  140.                 }
  141.             }
  142.             var category = item.getProperty("CATEGORIES");
  143.             if (displayElement("calendar-task-details-category-row", category && category.length)) {
  144.                 document.getElementById("calendar-task-details-category").value = category;
  145.             }
  146.             if (displayElement("calendar-task-details-entrydate-row", item.entryDate != null)) {
  147.                 document.getElementById("calendar-task-details-entrydate").value = dateFormatter.formatDateLong(item.entryDate);
  148.             }
  149.             if (displayElement("calendar-task-details-entrydate-row", item.entryDate != null)) {
  150.                 document.getElementById("calendar-task-details-entrydate").value = dateFormatter.formatDateLong(item.entryDate);
  151.             }
  152.             if (displayElement("calendar-task-details-duedate-row", item.dueDate != null)) {
  153.                 document.getElementById("calendar-task-details-duedate").value = dateFormatter.formatDateLong(item.dueDate);
  154.             }
  155.             var parentItem = item;
  156.             if (parentItem.parentItem != parentItem) {
  157.                 parentItem = parentItem.parentItem;
  158.             }
  159.             var recurrenceInfo = parentItem.recurrenceInfo;
  160.             if (displayElement("calendar-task-details-repeat-row", recurrenceInfo != null)) {
  161.                 var kDefaultTimezone = calendarDefaultTimezone();
  162.                 var startDate = item.entryDate ? item.entryDate.getInTimezone(kDefaultTimezone) : null;
  163.                 var endDate = item.dueDate ? item.dueDate.getInTimezone(kDefaultTimezone) : null;
  164.                 var detailsString = recurrenceRule2String(recurrenceInfo,startDate,endDate,startDate.isDate);
  165.                 if (detailsString) {
  166.                     document.getElementById("calendar-task-details-repeat").value = detailsString.split("\n").join(" ");
  167.                 }
  168.             }
  169.             var textbox = document.getElementById("calendar-task-details-description");
  170.             var description = item.hasProperty("DESCRIPTION") ? item.getProperty("DESCRIPTION") : null;
  171.             textbox.value = description;
  172.             textbox.inputField.readOnly = true;
  173.             var gURL = item.getProperty("URL");
  174.             if (displayElement("calendar-task-details-attachment-row", gURL && gURL.length)) {
  175.                 var urlLabel = document.getElementById("calendar-task-details-attachment");
  176.                 urlLabel.value = gURL;
  177.                 urlLabel.setAttribute("tooltiptext", gURL);
  178.             }
  179.         }
  180.     }
  181. };
  182.  
  183. function taskViewUpdate(filter) {
  184.  
  185.     var percentCompleted = function(item) {
  186.         var percent = 0;
  187.         var property = item.getProperty("PERCENT-COMPLETE");
  188.         if (property != null) {
  189.             var percent = parseInt(property);
  190.         }
  191.         return percent;
  192.     }
  193.  
  194.     var filterFunctions = {
  195.         notstarted: function filterNotStarted(item) {
  196.             return (percentCompleted(item) <= 0);
  197.         },
  198.         overdue: function filterOverdue(item) {
  199.           // in case the item has no due date
  200.           // it can't be overdue by definition
  201.           if (item.dueDate == null) {
  202.               return false;
  203.           }
  204.           return (percentCompleted(item) < 100) &&
  205.                  !(item.dueDate.compare(now()) > 0);
  206.         },
  207.         completed: function filterCompleted(item) {
  208.             return (percentCompleted(item) >= 100);
  209.         }
  210.     }
  211.  
  212.     var tree = document.getElementById("calendar-task-tree");
  213.     tree.filterFunction = filterFunctions[filter] || null;
  214.  
  215.     var todayDate = new Date();
  216.     var startDate = new Date(todayDate.getFullYear(),
  217.                              todayDate.getMonth(),
  218.                              todayDate.getDate(),
  219.                              0, 0, 0);
  220.  
  221.     var rangeFunctions = {
  222.         today: function rangeToday() {
  223.             tree.startDate = jsDateToDateTime(startDate)
  224.                 .getInTimezone(calendarDefaultTimezone());
  225.             tree.endDate = jsDateToDateTime(
  226.                 new Date(startDate.getTime() + (1000 * 60 * 60 * 24) - 1))
  227.                     .getInTimezone(calendarDefaultTimezone());
  228.         },
  229.         next7days: function rangeNext7Days() {
  230.             tree.startDate = jsDateToDateTime(startDate)
  231.                 .getInTimezone(calendarDefaultTimezone());
  232.             tree.endDate = jsDateToDateTime(
  233.                 new Date(startDate.getTime() + (1000 * 60 * 60 * 24 * 8)))
  234.                     .getInTimezone(calendarDefaultTimezone());
  235.         }
  236.     }
  237.     
  238.     if (rangeFunctions[filter]) {
  239.       rangeFunctions[filter]();
  240.     } else {
  241.       tree.startDate = null;
  242.       tree.endDate = null;
  243.     }
  244.  
  245.     tree.refresh();
  246. }
  247.  
  248. function taskViewUpdateFilter(event) {
  249.  
  250.     taskViewUpdate(event.target.value);
  251. }
  252.  
  253. function sendMailToOrganizer() {
  254.     var item = document.getElementById("calendar-task-tree").currentTask;
  255.     if (item != null) {
  256.         var organizer = item.organizer;
  257.         if (organizer) {
  258.             if (organizer.id && organizer.id.length) {
  259.                 var email = organizer.id;
  260.                 var re = new RegExp("^mailto:(.*)", "i");
  261.                 if (email && email.length) {
  262.                     if (re.test(email)) {
  263.                         email = RegExp.$1;
  264.                     } else {
  265.                         email = email;
  266.                     }
  267.                 }
  268.  
  269.                 // Set up the subject
  270.                 var emailSubject = calGetString("sun-calendar-event-dialog",
  271.                                                 "emailSubjectReply",
  272.                                                 [item.title]);
  273.  
  274.                 sendMailTo(email, emailSubject);
  275.             }
  276.         }
  277.     }
  278. }
  279.  
  280. function taskViewObserveDisplayDeckChange(event) {
  281.     var deck = event.target;
  282.  
  283.     // Bug 309505: The 'select' event also fires when we change the selected
  284.     // panel of calendar-view-box.  Workaround with this check.
  285.     if (deck.id != "displayDeck") {
  286.         return;
  287.     }
  288.  
  289.     var id = null;
  290.     try {
  291.       id = deck.selectedPanel.id
  292.     }
  293.     catch (e) {}
  294.  
  295.     // In case we find that the task view has been made visible, we refresh the view.
  296.     if (id == "calendar-task-box") {
  297.         taskViewUpdate(
  298.             document.getElementById("task-tree-filter").value || "all");
  299.     }
  300. }
  301.  
  302. document.getElementById("displayDeck").
  303.     addEventListener("select", taskViewObserveDisplayDeckChange, true);
  304.